<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>例子8-JS进阶（二）</title>
    </head>
    <body class="cols">
        <pre>
            <code>
                    // 例子8

                    var a = 20;
                    var foo = {
                        a: 10,
                        getA: function () {
                            return this.a;
                        }
                    }
                    console.log(foo.getA()); // 10

                    var test = foo.getA;
                    console.log(test()); // 20, test为调用者，独立调用
            </code>
        </pre>
        <pre>
            <code>
                    var b = 20;

                    function bar() {
                        return this.b;
                    }
                    var obj = {
                        b: 10,
                        getB: bar
                    }
                    console.log(obj.getB()); // 10，obj中的属性getB为调用者，为obj对象所拥有
            </code>
        </pre>
        <pre>
            <code>
                    function baz() {
                        console.log(this.c)
                    }

                    function active(fn) {
                        fn(); // 真实调用者，为独立调用
                    }

                    var c = 20;
                    var obj2 = {
                        c: 10,
                        getC: baz
                    }

                    active(obj2.getC); // 20
            </code>
        </pre>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/8.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>
</html>